package com.demo.java.OD351_400.OD353;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【 拔河比赛(B卷)】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/146164521
 */
public class OdMain {

    static class Player implements Comparable<Player> {
        int score;
        int id;

        public Player(int score, int id) {
            this.score = score;
            this.id = id;
        }

        @Override
        public int compareTo(Player other) {
            if (this.score == other.score) {
                return Integer.compare(other.id, this.id); // 如果分数相同，按照玩家ID降序排序
            } else {
                return Integer.compare(other.score, this.score); // 按照玩家分数降序排序
            }
        }
    }

    public static final int NUM_PLAYERS_TO_DISPLAY = 10;

    public static void main(String[] args) {
        List<Player> players = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.isEmpty()) {
                break; // 如果遇到空行，退出循环
            } else {
                String[] tokens = line.split("\\s+");
                if (tokens.length != 2) {
                    System.err.println("无效的输入格式。跳过此行。");
                } else {
                    int score = Integer.parseInt(tokens[0]);
                    int id = Integer.parseInt(tokens[1]);
                    players.add(new Player(score, id)); // 解析并将玩家数据添加到列表中
                }
            }
        }
        scanner.close();

        if (NUM_PLAYERS_TO_DISPLAY <= 0) {
            System.err.println("无效的玩家显示数量。");
            System.exit(1);
        }

        if (!players.isEmpty()) {
            // 只排序前N个玩家，而不是整个列表
            Collections.sort(players);
            int displayCount = Math.min(NUM_PLAYERS_TO_DISPLAY, players.size());

            // 显示前N个玩家
            for (int i = 0; i < displayCount; i++) {
                Player player = players.get(i);
                System.out.println(player.score + " " + player.id);
            }
        } else {
            // 处理没有玩家数据的情况
        }
    }
}

